package com.ykt.mall.ctrl;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.jfinal.aop.Before;
import com.jfinal.ext.route.ControllerBind;
import com.ykt.interceptor.Seach;
import com.ykt.model.Order;

@ControllerBind(controllerKey="/orders")//viewPath="/template"
public class OrderCtrl extends BaseWebController<Order> {
	
	public void index(){
	}
	
	public void book(){
		render("/template/book.html");
	}
	
	@Before(Seach.class)
	public void getOrderList(){
		
		Seach seach= getAttr("seachBean");
		seach.setOrderBy(" order by a.create_time desc ");
		filterCustomer(seach);
		
		
		int page = this.getParaToInt("pageindex",1);
		int rows = this.getParaToInt("pagesize",10);
		
		List<Order> orderList = Order.dao.paginate(page, rows, seach).getList();
		List<Map<String ,Object>> mapList = new ArrayList<Map<String ,Object>>();
		
		for(Order order : orderList){
			Map<String ,Object> map = new HashMap<String,Object>();
			map.put("order", order);
			map.put("orderList", order.getOrderDetails());
			
			mapList.add(map);
		}
		
		this.renderJson(mapList);
	}
	
	public void changeStatus(){
		
		Map<String,Object> map = new HashMap<String,Object>();
		
		Date date=new Date();

		Order orderParam = getModel(Order.class);
		try{
			Order order = Order.dao.findById(orderParam.getInt("id"));

			order.set("status",orderParam.getInt("status"));
			order.set("update_time", date);		

			order.update();

			map.put("Msg", "Ok");
		}catch(Exception e){
			map.put("Msg", "系统错误，请重新操作。");
		}
		
		renderJson(map);
	}
	public void view(){
		setAttr("po", Order.dao.findById(getParaToInt()));
	}
	
	
}
